package com.regulareport.web.controller.report;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.regulareport.common.core.domain.entity.SysUser;
import com.regulareport.common.utils.poi.ExcelUtil;
import com.regulareport.form.domain.ReportVerificationConfig;
import com.regulareport.form.service.IReportVerificationConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.regulareport.common.annotation.Log;
import com.regulareport.common.core.controller.BaseController;
import com.regulareport.common.core.domain.AjaxResult;
import com.regulareport.common.enums.BusinessType;
import com.regulareport.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 校验配置Controller
 * 
 * @author
 * @date 2025-10-08
 */
@RestController
@RequestMapping("/report/verify/config")
public class ReportVerificationConfigController extends BaseController
{

    private static final Logger log = LoggerFactory.getLogger(ReportVerificationConfigController.class);
    @Autowired
    private IReportVerificationConfigService reportVerificationConfigService;

    /**
     * 查询校验配置列表
     */
    @PreAuthorize("@ss.hasPermi('report:verify:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportVerificationConfig reportVerificationConfig)
    {
        startPage();
        List<ReportVerificationConfig> list = reportVerificationConfigService.selectReportVerificationConfigList(reportVerificationConfig);
        return getDataTable(list);
    }

    /**
     * 导出校验配置列表
     */
    @PreAuthorize("@ss.hasPermi('report:verify:export')")
    @Log(title = "校验配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportVerificationConfig reportVerificationConfig)
    {
        List<ReportVerificationConfig> list = reportVerificationConfigService.selectReportVerificationConfigList(reportVerificationConfig);
        ExcelUtil<ReportVerificationConfig> util = new ExcelUtil<ReportVerificationConfig>(ReportVerificationConfig.class);
        util.exportExcel(response, list, "校验规则");
    }

    /**
     * 获取校验配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:verify:query')")
    @GetMapping(value = "/{pkId}")
    public AjaxResult getInfo(@PathVariable("pkId") Long pkId)
    {
        return success(reportVerificationConfigService.selectReportVerificationConfigByPkId(pkId));
    }

    /**
     * 新增校验配置
     */
    @PreAuthorize("@ss.hasPermi('report:verify:add')")
    @Log(title = "校验配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportVerificationConfig reportVerificationConfig)
    {
        log.info("校验配置新增方法开始： {}" , reportVerificationConfig.toString());
        reportVerificationConfig.setCreateBy(getUsername());
        reportVerificationConfig.setEffectiveSign("01"); //新增默认有效
        reportVerificationConfig.setDelFlag("0");
        return toAjax(reportVerificationConfigService.insertReportVerificationConfig(reportVerificationConfig));
    }

    /**
     * 修改校验配置
     */
    @PreAuthorize("@ss.hasPermi('report:verify:edit')")
    @Log(title = "校验配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportVerificationConfig reportVerificationConfig)
    {
        reportVerificationConfig.setUpdateBy(getUsername());
        return toAjax(reportVerificationConfigService.updateReportVerificationConfig(reportVerificationConfig));
    }

    /**
     * 删除校验配置
     */
    @PreAuthorize("@ss.hasPermi('report:verify:delete')")
    @Log(title = "校验配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{pkId}")
    public AjaxResult remove(@PathVariable Long pkId)
    {
        return toAjax(reportVerificationConfigService.deleteReportVerificationConfigByPkId(pkId));
    }

    @Log(title = "校验配置", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('report:verify:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ReportVerificationConfig> util = new ExcelUtil<ReportVerificationConfig>(ReportVerificationConfig.class);
        List<ReportVerificationConfig> verificationConfigList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = reportVerificationConfigService.importVerificationConfig(verificationConfigList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ReportVerificationConfig> util = new ExcelUtil<ReportVerificationConfig>(ReportVerificationConfig.class);
        util.importTemplateExcel(response, "校验规则配置模板");
    }
}
